package com.wechat.bot.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhangyanan
 * @date 2022/9/9 10:38
 * @description'
 */
@Configuration
public class RabbitConfig {

    @Value("${spring.rabbitmq.exchangename}")
    String changename;

    @Value("${spring.rabbitmq.queue}")
    String queuename;

    @Value("${spring.rabbitmq.routingname}")
    String routingname;

    @Bean
    public Queue myQueue(){
        return new Queue(queuename, true, false, false);
    }
//    @Bean
//    public Queue myQueue1(){
//        return new Queue("myqueue1");
//    }

    @Bean
    public Exchange myExchange(){
        return new DirectExchange(changename, true, false, null);
    }

    @Bean
    public Binding myBinding() {
    // 绑定的目的地，绑定的类型：到交换器还是到队列，交换器名称，路由key，绑定的属性
    // new Binding("", Binding.DestinationType.EXCHANGE, "", "",null);
    // 绑定的目的地，绑定的类型：到交换器还是到队列，交换器名称，路由key绑定的属性
    // new Binding("", Binding.DestinationType.QUEUE, "", "",null);
    // 绑定了交换器direct.biz.ex到队列myqueue，路由key是direct.biz.ex
        return new Binding(
                queuename,
            Binding.DestinationType.QUEUE,
                changename,
                routingname,
            null);
    }

//    @Bean
//    public Binding myBinding1() {
//    // 绑定的目的地，绑定的类型：到交换器还是到队列，交换器名称，路由key，绑定的属性
//    // new Binding("", Binding.DestinationType.EXCHANGE, "", "",null);
//    // 绑定的目的地，绑定的类型：到交换器还是到队列，交换器名称，路由key绑定的属性
//    // new Binding("", Binding.DestinationType.QUEUE, "", "",null);
//    // 绑定了交换器direct.biz.ex到队列myqueue，路由key是direct.biz.ex
//        return new Binding(
//            "myqueue1",
//            Binding.DestinationType.QUEUE,
//            "myex",
//            "direct.biz.ex1",
//            null);
//    }



//    //交换机名称
//    @Value("${rabbitmq.direct.exchangeName}")
//    private String directExchangeName;
//    //队列集合
//    @Value("#{'${rabbitmq.direct.queueName}'.split(',')}")
//    private List<Queue> queueNames;
//    //解除绑定队列名称
//    @Value("${rabbitmq.direct.unbindQueueName}")
//    private List<String> unbindQueueNames;
//
//    @Autowired
//    private RabbitTemplate rabbitTemplate;
//    @Bean
//    public DirectExchange directExchange(){
//        return new DirectExchange(directExchangeName);
//    }
//
//
//    @Override
//    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//         ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
//         //解除队列绑定（不删除队列，可根据需求删除不需要的队列）
////         unbindQueueNames.forEach(queueName -> rabbitTemplate.execute(
////                 channel -> {
////                     channel.queueUnbind(queueName, directExchangeName, "", Collections.emptyMap());
////                     //声明队列
////                     channel.queueDeclare(queueName, true, false, false, null);
////                     //一次消费的数量
////                     channel.basicQos(Integer.valueOf(100));
////                 }
////         ));
////         queueNames.forEach(queue -> {
////             //增加队列
////             beanFactory.registerSingleton("direct_" + queue.getName() , queue);
////             //绑定队列到交换机
////             beanFactory.registerSingleton("direct_" + queue.getName() + "_Binding", BindingBuilder.bind(queue).to(directExchange()));
////         });
//
//         unbindQueueNames.forEach(queueName -> rabbitTemplate.execute(
//                 channel -> channel.queueUnbind(queueName, directExchangeName, "", Collections.emptyMap())
//         ));
//
//         queueNames.forEach(queue -> {
//             //增加队列
//             beanFactory.registerSingleton("direct_" + queue.getName() , queue);
//             //绑定队列到交换机
//             beanFactory.registerSingleton("direct_" + queue.getName() + "_Binding", BindingBuilder.bind(queue).to(directExchange()));
//         });
//
////         queueNames.forEach(queue -> rabbitTemplate.execute(channel -> {
////                //声明队列
////                AMQP.Queue.DeclareOk declareOk = channel.queueDeclare(queue.getName(), true, false, false, null);
////                //一次消费的数量
////                channel.basicQos(Integer.valueOf(100));
////                return declareOk;
////         }));
//    }
}
